// packageE/location/location.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    datas: {
      type: null
    },
    component_id: {
      type: null
    },
    page_id: {
      type: null
    }
  },
  data: {
    totalUnreadMessageCount:0,
    address: "",
    point: {
      lat: '',
      lng: ''
    },
    city: ""
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {

    },
    moved() { },
    detached() { },
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() { },
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    let totalUnreadMessageCount = app.globalData.TUIKit ? app.globalData.TUIKit.getTotalUnreadMessageCount() : 0
      this.setData({
        totalUnreadMessageCount:typeof totalUnreadMessageCount === 'number' ? totalUnreadMessageCount : 0
      });
    this._getLocation((mapdata, e) => {
      this.setData({
        address: mapdata.address,
        city: mapdata.address_component.city ? mapdata.address_component.city : mapdata.address_component.province,
        point: e
      });
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() { 
      let totalUnreadMessageCount = app.globalData.TUIKit ? app.globalData.TUIKit.getTotalUnreadMessageCount() : 0
      this.setData({
        totalUnreadMessageCount:typeof totalUnreadMessageCount === 'number' ? totalUnreadMessageCount : 0
      });
    },
    hide() { },
    resize() { },
  },

  methods: {
    tolocation() {
      const pages = getCurrentPages();
      const currentPage = pages[pages.length - 1];
      const url = `/${currentPage.route}?page_id=${this.data.page_id}`;
      wx.setStorageSync('diyurl', url);
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation/o2oLocation?tag=diy'
      });

    },
    tosearch() {
      if (app.globalData.store_alone_temp == 1) {
        // 开启门店独立模块
        wx.navigateTo({
          url: '/packageC/o2o/StoreSearch/StoreSearch?store_id=' + app.globalData.STORE_ID
        });
        return;
      }
      if (!this.data.city) {
        wx.showToast({
          icon: 'none',
          title: '请先选择城市',
          duration: 1000
        });
        return;
      }
      let search_type = this.data.datas.search_type || 'store';
      wx.navigateTo({
        url: '/packageC/o2o/o2oSearchMultiple/o2oSearchMultiple?city=' + this.data.city + '&point=' + JSON.stringify(this.data.point) + '&search_type=' + search_type
      });
    },

    /**
     * 跳转到消息页面
     */
    gotoNotice: function () {
      wx.navigateTo({
        url: "/TUI-CustomerService/pages/index"
      });

    }
  }
});
